﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;
using System.IO;//Dung de Doc Chuoi XML
using System.Text.RegularExpressions;

namespace QuanLyNhapXuat.Presentation.WinForm
{
    public partial class frm_NhanVien : Telerik.WinControls.UI.RadForm
    {
        static frm_NhanVien doituong = null;
        public static frm_NhanVien TaoDoiTuong()
        {
            if (doituong == null)
                doituong = new frm_NhanVien();
            return doituong;
        }
        protected frm_NhanVien()
        {
            InitializeComponent();
        }
        //tao bien
        private string ho;
        private string ten;
        //private bool them = false;
        //bo di cac Lop trong DaTaAccess va BussinessLayer
        //Dau tien tao ra Webservice Nhan Vien
        
        private void btn_LuuNhanh_Click(object sender, EventArgs e)
        {

        }

        private void btn_Dong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_XacNhan_Click(object sender, EventArgs e)
        {                     

            string ma;
            if (cbb_chucvu.SelectedIndex==0)
            {
                DataSet tam = new DataSet();
                tam.ReadXml(new StringReader(sr_NhanVien.SelectSQL("select top 1 manv from nhanvien where manv like 'NVNhap%' order by manv desc")));
                DataTable bang = tam.Tables[0];
                string ma_tam = bang.Rows[0][0].ToString();
                int so=int.Parse(ma_tam.Substring(6,4).ToString())+1;
                ma = "NVNhap" + so.ToString("0000");
                txt_MaNV.Text = ma.ToString();
            }
            if (cbb_chucvu.SelectedIndex==1)
            {
                DataSet tam = new DataSet();
                tam.ReadXml(new StringReader(sr_NhanVien.SelectSQL("select top 1 manv from nhanvien where manv like 'NVGiao%' order by manv desc")));
                DataTable bang = tam.Tables[0];
                string ma_tam = bang.Rows[0][0].ToString();
                int so = int.Parse(ma_tam.Substring(6, 4).ToString()) + 1;
                ma = "NVGiao" + so.ToString("0000"); 
                txt_MaNV.Text = ma.ToString();
            }
            if (cbb_chucvu.SelectedIndex==2)
            {
                DataSet tam = new DataSet();
                tam.ReadXml(new StringReader(sr_NhanVien.SelectSQL("select top 1 manv from nhanvien where manv like 'QuanLi%' order by manv desc")));
                DataTable bang = tam.Tables[0];
                string ma_tam = bang.Rows[0][0].ToString();
                int so = int.Parse(ma_tam.Substring(6, 4).ToString()) + 1;
                ma = "QuanLi" + so.ToString("0000");
                txt_MaNV.Text=ma.ToString();
            }
            string manv = txt_MaNV.Text;
            string honv = txt_HoNV.Text;
            string tennv = mcbb_tennv.Text ;
            string diachi = txt_DiaChi.Text;
            string sdt=txt_SDT.Text;
            string chucvu=cbb_chucvu.Text;
            bool gioitinh;
            if (rdb_Nu.Checked==true)
            {
                gioitinh = true;
            }
            else
            {
                gioitinh = false;
            }
            try
            {
                sr_NhanVien.Them(manv, honv, tennv, gioitinh, diachi, sdt, chucvu);
            }
            catch (SqlException sqlex)
            {
                MessageBox.Show("Lỗi kết nối: " + sqlex.Message);
            }
            catch (ApplicationException appex)
            {
                MessageBox.Show("Lỗi ứng dụng: " + appex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi chương trình:" + ex.Message);
            }
            
            
            txt_MaNV.Clear();
            txt_HoNV.Clear();
            //mcbb_tennv.DataSource = null;
            txt_DiaChi.Clear();
            mcbb_tennv.Text = "";
            txt_SDT.Clear();
            //gridview_DanhSachNV.Rows.Add(manv, honv, tennv, gioitinh, diachi, sdt, chucvu);
            MessageBox.Show("Lưu thành công!");
            loadNV();
        }
        private bool ktrong(string ho, string ten, string dc, string sdt)
        {
            if (ho.Trim()==""&&ten.Trim()==""&&dc.Trim()==""&&sdt.Trim()=="")
            {
                return false;
                
            }
            return true;
        }
        DataSet dsNV = new DataSet();
        QuanLyNhapXuat.SR_XLNhanVien.WS_NhanVienSoapClient sr_NhanVien = new QuanLyNhapXuat.SR_XLNhanVien.WS_NhanVienSoapClient();
        private void frm_NhanVien_Load(object sender, EventArgs e)
        {
            
            //Chu y ky phan lay Danh Sach Theo Kieu XML nay nhe            
            //dsNV.ReadXml(new StringReader(sr_NhanVien.LayDS()));//Da lay Danh Sach thong qua XML rui do
            //Sao do dua vao muilcombobox do
            //Tam thoi tao dua len GripView truoc
            loadNV();
            RadMultiColumnComboBoxElement multiColumnComboElement = this.mcbb_tennv.MultiColumnComboBoxElement;
            multiColumnComboElement.DropDownSizingMode = SizingMode.UpDownAndRightBottom;
            multiColumnComboElement.DropDownMinSize = new Size(300, 200);
            
        }

        private void txt_SDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;

            }
        }
        private void loadNV()
        {
            dsNV.Clear();
            try
            {
                dsNV.ReadXml(new StringReader(sr_NhanVien.LayDS()));
                mcbb_tennv.DataSource = dsNV.Tables[0];
                mcbb_tennv.DisplayMember = "tennv";
                mcbb_tennv.ValueMember = "manv";
                mcbb_tennv.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                
                MessageBox.Show("Lỗi"+ ex.Message);
            }
            //mcbb_tennv.Text = null;
        }

        private void mcbb_tennv_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mcbb_tennv.SelectedIndex>-1&&mcbb_tennv.Text.Trim()!="")
            {
                btn_them.Enabled = true;
                //btn_LuuToanBo.Enabled = false;
                //btn_HuyNhap.Enabled = false;
                btn_ThayDoi.Enabled = true;
                btn_ThayDoi.Visible=true;
                btn_them.Visible = true;
                txt_HoNV.Enabled = false;
                rdb_Nam.Enabled = false;
                rdb_Nu.Enabled = false;
                txt_DiaChi.Enabled = false;
                txt_SDT.Enabled = false;
                cbb_chucvu.Enabled = false;
                DataSet dsnew = new DataSet();
                dsnew.ReadXml(new StringReader(sr_NhanVien.SelectSQL("select manv,honv,tennv,gioitinh,diachi,sdt,chucvu from nhanvien where manv='"+mcbb_tennv.SelectedValue+"'")));
                DataTable bang = dsnew.Tables[0];
                DataTableReader dtbr = bang.CreateDataReader();
                while (dtbr.Read())
	            {
            	    txt_MaNV.Text=dtbr[0].ToString();
                    txt_HoNV.Text=dtbr[1].ToString();
                    mcbb_tennv.Text=dtbr[2].ToString();
                    rdb_Nam.Checked=bool.Parse(dtbr[3].ToString());
                    txt_DiaChi.Text = dtbr[4].ToString();
                    txt_SDT.Text=dtbr[5].ToString();
                    cbb_chucvu.Text = dtbr[6].ToString();
	            }
            }
        }

        private void rdb_Nam_CheckedChanged(object sender, EventArgs e)
        {
            rdb_Nu.Checked =! rdb_Nam.Checked;
        }

        private void btn_them_Click(object sender, EventArgs e)
        {
            btn_ThayDoi.Visible = true;
            //btn_LuuToanBo.Enabled = true;
            //btn_HuyNhap.Enabled = true;
            btn_ThayDoi.Enabled = false;
            btn_them.Visible = false;
            txt_MaNV.Clear();
            txt_HoNV.Clear();
            //mcbb_tennv.DataSource = null;
            txt_DiaChi.Clear();
            cbb_chucvu.DataSource = null;
            txt_SDT.Clear();

            txt_HoNV.Enabled = true;
            rdb_Nam.Enabled = true;
            rdb_Nu.Enabled = true;
            txt_DiaChi.Enabled = true;
            txt_SDT.Enabled = true;
            cbb_chucvu.Enabled = true;
        }

        private void btn_ThayDoi_Click(object sender, EventArgs e)
        {
            try
            {
                btn_them.Visible = true;
                btn_them.Enabled = true;
                btn_ThayDoi.Visible = false;
                //btn_LuuToanBo.Enabled = false;
                //btn_HuyNhap.Enabled = false;
                txt_HoNV.Enabled = true;
                txt_DiaChi.Enabled = true;
                txt_SDT.Enabled = true;
                cbb_chucvu.Enabled = true;
                rdb_Nam.Enabled = true;
                rdb_Nu.Enabled = true;
            }
            catch (Exception ex)
            {          
                
            }
            
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            string manv = txt_MaNV.Text;
            string honv = txt_HoNV.Text;
            string tennv = mcbb_tennv.Text;
            string diachi = txt_DiaChi.Text;
            string sdt = txt_SDT.Text;
            string chucvu = cbb_chucvu.Text;
            bool gioitinh;
            if (rdb_Nu.Checked == true)
            {
                gioitinh = true;
            }
            else
            {
                gioitinh = false;
            }
            try
            {
                sr_NhanVien.Sua(manv, honv, tennv, gioitinh, diachi, sdt, chucvu);
            }
            catch (SqlException sqlex)
            {
                MessageBox.Show("Lỗi kết nối: " + sqlex.Message);
            }
            catch (ApplicationException appex)
            {
                MessageBox.Show("Lỗi ứng dụng: " + appex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi chương trình:" + ex.Message);
            }
            MessageBox.Show("Lưu thành công!");
            loadNV();
        }

        private void txt_HoNV_Leave(object sender, EventArgs e)
        {
            if (txt_HoNV.Text.Trim()!="")
            {
                string patern = @"^\w[^0-9]+$";
                Regex rg = new Regex(patern);
                Match m = rg.Match(txt_HoNV.Text);
                if (!m.Success)
                {
                    MessageBox.Show("Họ chỉ được nhập chữ !!!");
                    txt_HoNV.Focus();
                }
            }
            
        }

        private void mcbb_tennv_Leave(object sender, EventArgs e)
        {
            if (mcbb_tennv.Text.Trim() != "")
            {
                string patern = @"^\w[^0-9]+$";
                Regex rg = new Regex(patern);
                Match m = rg.Match(mcbb_tennv.Text);
                if (!m.Success)
                {
                    MessageBox.Show("Tên chỉ được nhập chữ !!!");
                    mcbb_tennv.Focus();
                }
            }
            
        }

        private void txt_SDT_Leave(object sender, EventArgs e)
        {
            if (txt_SDT.Text.Trim() != "")
            {
                string patern = @"^\d{8,11}$";
                Regex rg = new Regex(patern);
                Match m = rg.Match(txt_SDT.Text);
                if (!m.Success)
                {
                    MessageBox.Show("SDT chỉ được nhập số !!!");
                    txt_SDT.Focus();
                }
            }
        }


    }
}
